package com.citycloud.ccuap.tc.sync.aspect;

import com.citycloud.ccuap.commons.json.JSONData;
import com.citycloud.ccuap.commons.sys.deptment.DeptmentService;
import com.citycloud.ccuap.commons.sys.entity.SysDeptment;
import com.citycloud.ccuap.commons.sys.entity.SysUser;
import com.citycloud.ccuap.commons.sys.user.UserService;
import com.citycloud.ccuap.tc.sync.dto.DepartmentMessageDTO;
import com.citycloud.ccuap.tc.sync.dto.UserMessageDTO;
import com.citycloud.ccuap.tc.sync.constant.GlobalSyncOperation;
import com.citycloud.ccuap.tc.sync.constant.GlobalSyncType;
import com.citycloud.ccuap.tc.sync.dto.SyncDataDto;
import com.citycloud.ccuap.tc.sync.service.IMessageService;
import com.google.common.collect.ImmutableMap;
import org.apache.commons.collections.CollectionUtils;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

/**
 * @author zjl
 * @date 2020/04/28 18:34
 */
@Aspect
@Component
public class DeptImportAspect {
    @Autowired
    private DeptmentService deptmentService;

    @Autowired
    private IMessageService messageService;

    @Autowired
    private UserService userService;

    @Pointcut("execution(* com.citycloud.ccuap.commons.sys.deptment.DeptmentController.importDeptment())")
    public void importDept() {
    }

    @AfterReturning(value = "importDept()", returning = "key")
    public void around(JoinPoint joinPoint, Object key) throws Throwable {
        if (key instanceof JSONData) {
            JSONData jsonData = (JSONData) key;
            ImmutableMap<String, Object> data = (ImmutableMap) jsonData.getData();
            boolean importPerson = (boolean) data.get("importPerson");
            Long projectId = (Long) data.get("projectId");
            List<Long> deptIdList = (List<Long>) data.get("deptIdList");
            sendImportMessage(importPerson, deptIdList, projectId);
        }
    }

    private void sendImportMessage(boolean importPerson, List<Long> deptIds, Long projectId) {
        //JMS 导入部门信息推送（消息推送）去重
        if (CollectionUtils.isNotEmpty(deptIds)) {
            List<SysDeptment> departments = deptmentService.selectBatchIds(deptIds);
            List<DepartmentMessageDTO> deptMessage = new ArrayList<>();
            List<UserMessageDTO> userMessage = new ArrayList<>();

            for (SysDeptment dept : departments) {
                DepartmentMessageDTO deptMessageDTO = new DepartmentMessageDTO();
                deptMessageDTO.build(dept);
                deptMessageDTO.setProjectId(projectId);
                deptMessage.add(deptMessageDTO);

                if (importPerson) {
                    List<SysUser> list = userService.findByDeptIdAndProjectIdIsNull(dept.getDeptId());
                    if (!list.isEmpty()) {
                        list.forEach(e -> {
                            UserMessageDTO userMessageDTO = new UserMessageDTO();
                            userMessageDTO.build(e);
                            userMessageDTO.setProjectId(projectId);
                            userMessage.add(userMessageDTO);
                        });
                    }
                }
            }
            sendDeptMessage(projectId, deptMessage);
            if (importPerson && !userMessage.isEmpty()) {
                sendUserMessage(projectId, userMessage);
            }

        }
    }

    private void sendDeptMessage(Long projectId, List<DepartmentMessageDTO> list) {
        SyncDataDto<List<DepartmentMessageDTO>> result = SyncDataDto.of(GlobalSyncType.OBJECT_TYPE_DEPT,
                GlobalSyncOperation.CREATED, projectId, list);
        messageService.sendMessageByTopic(result,GlobalSyncType.TOPIC_USER_DEPT);
    }

    private void sendUserMessage(Long projectId, List<UserMessageDTO> list) {
        SyncDataDto<List<UserMessageDTO>> result = SyncDataDto.of(GlobalSyncType.OBJECT_TYPE_USER,
                GlobalSyncOperation.CREATED, projectId, list);
        messageService.sendMessageByTopic(result,GlobalSyncType.TOPIC_USER_DEPT);
    }
}
